package com.lottery.im.module.chat.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.SetTop;
import com.lottery.im.module.tim.model.Conversation;
import com.lottery.im.module.tim.utils.TimTimeUtil;
import com.lottery.im.util.transformations.CropCircleTransformation;
import java.util.ArrayList;
import java.util.List;

/*
 * 会话列表 adapter
 * */
public class ConversationListAdapter extends BaseQuickAdapter<Conversation, BaseViewHolder> {

  private int mLongClickPosition = -1;

  private List<SetTop> mSetTops = new ArrayList<>();

  public ConversationListAdapter(@Nullable List<Conversation> data) {
    super(R.layout.item_conversation_list, data);
  }

  @Override protected void convert(BaseViewHolder helper, Conversation item) {

    ImageView ivPortrait = helper.getView(R.id.iv_portrait);

    Glide.with(mContext)
        .load(item.getAvatarUrl())
        .apply(new RequestOptions().bitmapTransform(new CropCircleTransformation())
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(ivPortrait);

    helper.setText(R.id.tv_name, item.getName());
    helper.setText(R.id.tv_summary, item.getLastMessageSummary());
    helper.setText(R.id.tv_time, TimTimeUtil.getTimeStr(item.getLastMessageTime()));
    long unRead = item.getUnreadNum();

    MsgView tvNum = helper.getView(R.id.tv_unread_num);
    if (unRead <= 0) {
      helper.setVisible(R.id.tv_unread_num, false);
    } else {
      UnreadMsgUtils.show(tvNum, (int) unRead);
    }

    if (mLongClickPosition == helper.getLayoutPosition()) {
      helper.setBackgroundColor(R.id.layout_item,
          ContextCompat.getColor(mContext, R.color.conversation_white_pressed));
    } else {
      boolean isTop = false;
      if (null != mSetTops) {
        for (SetTop setTop : mSetTops) {
          if (setTop.getUserId().equals(MyApplication.getInstance().getUserInfo().getUserId())
              && setTop.getId().equals(item.getIdentify())) {
            isTop = true;
            break;
          }
        }
      }

      if (isTop) {
        helper.setBackgroundRes(R.id.layout_item, R.drawable.conversation_settop_click_bg);
      } else {
        helper.setBackgroundRes(R.id.layout_item, R.drawable.conversation_white_click_bg);
      }
    }
  }

  public void setLongClickPosition(int position) {
    mLongClickPosition = position;
    notifyItemChanged(position);
  }

  public void cancelLongClickPosition(int position) {
    mLongClickPosition = -1;
    notifyItemChanged(position);
  }

  public void setSettopId(List<SetTop> setTops) {
    this.mSetTops = setTops;
  }
}
